package com.itheima.controller;


import com.itheima.common.PageResult;
import com.itheima.common.ResultData;
import com.itheima.domain.PmsProductAttribute;
import com.itheima.manager.PmsProductAttributeManager;
import com.itheima.vo.ProductAttrInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
public class PmsProductAttributeController {
    @Autowired
    PmsProductAttributeManager pmsProductAttributeManager;

    //查询商品
    @GetMapping("/productAttribute/attrInfo/{productCategoryId}")
    public ResultData attrInfo(@PathVariable Integer productCategoryId){
        List<ProductAttrInfo> productAttrInfo = pmsProductAttributeManager.attrInfo(productCategoryId);
        return ResultData.ok(productAttrInfo, "");
    }

    //根据分类查询属性列表或参数列表
    @GetMapping("/productAttribute/list/{cid}")
    public ResultData list(@PathVariable Integer cid, Integer type,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           @RequestParam(defaultValue = "1") Integer pageNum){
        PageResult pageResult = pmsProductAttributeManager.list(cid,type,pageSize,pageNum);
        return ResultData.ok(pageResult, "");
    }

    //查询单个商品属性
    @GetMapping("/productAttribute/{id}")
    public ResultData getById(@PathVariable Integer id){
        PmsProductAttribute pmsProductAttribute=pmsProductAttributeManager.getById(id);
        return ResultData.ok(pmsProductAttribute, "");
    }

    //添加商品属性信息
    @PostMapping("/productAttribute/create")
    public ResultData creat(@RequestBody PmsProductAttribute pmsProductAttribute) {
        pmsProductAttributeManager.creat(pmsProductAttribute);
        return ResultData.ok(null, "");
    }


    //批量删除商品属性
    @PostMapping("/productAttribute/delete")
    public ResultData delete(@RequestParam("ids") List<Integer>ids){
        pmsProductAttributeManager.delete(ids);
        return ResultData.ok(null, "");
    }
    //修改商品属性信息
    @PostMapping("/productAttribute/update/{id}")
    public ResultData update(@PathVariable Integer id,
                             @RequestBody PmsProductAttribute productAttribute) {
        pmsProductAttributeManager.update(id,productAttribute);
        return ResultData.ok(null, "");
    }

}
